package org.footballmanager.pandafootball.system.controller;

import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.form.impl.FootZjSearchForm;
import org.footballmanager.pandafootball.system.service.FootUserService;
import org.footballmanager.pandafootball.system.service.FootZjService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@SessionAttributes("footZjSearchForm")
@RequestMapping("/foot/footZj")
public class FootZjController extends BaseController{
	/* 实体名称 */
	private static final String ENTITYNAME = "footZj";
	private static final String LISTPAGE = String.format(Constants.LISTPAGE, Constants.FOOT, ENTITYNAME);
	private static final String VIEWPAGE = String.format(Constants.VIEWPAGE, Constants.FOOT, ENTITYNAME);
	private static final String REDIRECT = String.format(Constants.REDIRECT, Constants.FOOT, ENTITYNAME);

	@Autowired
	private FootZjService footZjService;
	@Autowired
	private FootUserService footUserService;

	/**
	 * 初始化
	 * @param map 参数
	 * @param footZjSearchForm 条件参数
	 */
	@ModelAttribute
	public void init(Model map, @ModelAttribute FootZjSearchForm footZjSearchForm){
		map.addAttribute("userList", footUserService.getall());
		FootZjSearchForm newFootZjSearchForm = new FootZjSearchForm();
		if(footZjSearchForm != null){
			newFootZjSearchForm = footZjSearchForm;
		}
		map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), newFootZjSearchForm);
	}

	@RequestMapping(value="/search")
	public String search(Model map, @ModelAttribute FootZjSearchForm footZjSearchForm) {
        footZjService.search(ENTITYNAME, map, footZjSearchForm);
		return LISTPAGE;
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public String delete(@RequestParam Long id, HttpServletRequest request) {
		footZjService .delete(id, request);
		return REDIRECT;
	}

	@RequestMapping(value = "/deleteall", method = RequestMethod.POST)
	public String deleteall(HttpServletRequest request, @ModelAttribute FootZjSearchForm footZjSearchForm) {
        footZjService.deleteAll(footZjSearchForm, request);
		return REDIRECT;
	}
	
	@RequestMapping(value = "/view", method = RequestMethod.GET)
	public String openViewPage(Model map, @RequestParam Long id, @ModelAttribute FootZjSearchForm footZjSearchForm) {
		map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), footZjSearchForm);
		map.addAttribute(ENTITYNAME, footZjService.get(id));
		return VIEWPAGE;
	}	
		
}
